package com.jeesite.modules.merchant.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.car.entity.Car;
import com.jeesite.modules.general.CosUtil;
import com.jeesite.modules.merchant.entity.Merchant;
import com.jeesite.modules.merchant.service.MerchantService;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Controller
@RequestMapping(value = "${adminPath}/merchant/merchant")
public class MerchantController extends BaseController {

    @Autowired
    private MerchantService merchantService;

    @ModelAttribute
    public Merchant get(String id, boolean isNewRecord) {
        return merchantService.get(id, isNewRecord);
    }

    @PostMapping(value = "saveMerchantData")
    @ResponseBody
    public String saveMerchantData(@RequestParam(value = "files", required = false) MultipartFile[] files, @Validated Merchant merchant){
        if (!Objects.isNull(files)) {
            // 根据商品名称  查询是否存在重复名称
            String status = merchantService.judgeMerchantName(merchant.getName());
            if (status != null && !status.equals("")){
                if (status.equals("0")){
                    return renderResult(Global.TRUE, text("该商户名称已申请，请等待管理员审核！"));
                }else if (status.equals("1")){
                    return renderResult(Global.TRUE, text("该商户名称已申请！"));
                }else if (status.equals("2")){
                    return renderResult(Global.TRUE, text("该商户名称已申请，并管理员已驳回申请！"));
                }
            }
            // 查询该手机号是否已注册过
            Integer integer = merchantService.judgeUserRepeat(merchant.getPhone());
            if (integer > 0){
                return renderResult(Global.TRUE, text("该手机号已注册，无法再次注册！"));
            }
            for (MultipartFile file : files) {
                String[] name = (FilenameUtils.getName(file.getOriginalFilename())).split("_");
                if("cover".equals(name[0])){
                    JSONObject result = CosUtil.uploadFile(file, "", "license");
                    if(!result.getBoolean("result")){
                        merchant.setLicenseUrl(result.getString("fileName2"));
                    }else {
                        return renderResult(Global.TRUE, text("营业执照上传失败！"));
                    }
                }
                if ("logo".equals(name[0])){
                    JSONObject result = CosUtil.uploadFile(file, "", "logo");
                    if(!result.getBoolean("result")){
                        merchant.setLogoUrl(result.getString("fileName2"));
                    }else {
                        return renderResult(Global.TRUE, text("商家LOGO上传失败！"));
                    }
                }
            }
            merchant.setReviewStatus("0");
            merchantService.insert(merchant);
            return renderResult(Global.TRUE, text("保存数据成功！"));
        }else{
            return renderResult(Global.FALSE, text("图片未上传！"));
        }
    }

    /**
     * 列表
     * @param merchant
     * @param model
     * @return
     */
    @RequiresPermissions("merchant:merchant:view")
    @RequestMapping(value = {"list", ""})
    public String list(Merchant merchant, Model model) {
        model.addAttribute("merchant", merchant);
        return "modules/merchant/merchantList";
    }

    @RequiresPermissions("merchant:merchant:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<Merchant> listData(Merchant merchant, HttpServletRequest request, HttpServletResponse response) {
        merchant.setPage(new Page<>(request, response));
        Page<Merchant> page = merchantService.findPage(merchant);
        return page;
    }

    @RequiresPermissions("merchant:merchant:edit")
    @RequestMapping(value = "formEdit")
    public String formEdit(Merchant merchant, Model model) {
        model.addAttribute("merchant", merchant);
        model.addAttribute("baseUrl", CosUtil.URL);
        return "modules/merchant/merchantForm";
    }

    /**
     * 商户信息审核
     * @param merchant
     * @return
     */
    @RequiresPermissions("merchant:merchant:edit")
    @PostMapping(value = "reviewData")
    @ResponseBody
    public String reviewData(@Validated Merchant merchant) {
        merchantService.reviewData(merchant);
        return renderResult(Global.TRUE, text("审核完成！"));
    }

}
